<template>
    <div>
        <!--行内表单-->
        <el-form :inline="true">
            <el-form-item>
                <el-button type="primary" @click="dialogVisible = true">新增</el-button>
            </el-form-item>
        </el-form>
        <!--树形数据表格-->
        <el-table
                :data="tableData"
                style="width: 100%;margin-bottom: 20px;"
                row-key="id"
                border
                stripe
                default-expand-all
                :header-cell-style="{'text-align':'center'}"
                :tree-props="{children: 'children', hasChildren: 'hasChildren'}">
            <el-table-column
                    prop="name"
                    label="名称"
                    sortable
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="perms"
                    label="权限编码"
                    sortable
                    width="180">
            </el-table-column>
            <el-table-column
                    prop="icon"
                    label="图标">
            </el-table-column>
            <el-table-column
                    prop="type"
                    width="80"
                    align="center"
                    label="类型">
                <template slot-scope="scope">
                    <el-tag size="medium" v-if="scope.row.type === 0">目录</el-tag>
                    <el-tag size="medium" v-else-if="scope.row.type === 1" type="success">菜单</el-tag>
                    <el-tag size="medium" v-else-if="scope.row.type === 2" type="info">按钮</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    prop="path"
                    label="菜单URL">
            </el-table-column>
            <el-table-column
                    prop="component"
                    label="菜单组件">
            </el-table-column>
            <el-table-column
                    prop="orderNum"
                    width="80"
                    align="center"
                    label="排序号">
            </el-table-column>
            <el-table-column
                    prop="status"
                    width="80"
                    align="center"
                    label="状态">
                <template slot-scope="scope">
                    <el-tag size="medium" type="success" v-if="scope.row.status === 1">正常</el-tag>
                    <el-tag size="medium" type="danger" v-else-if="scope.row.status === 0">禁用</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                    width="110"
                    align="center"
                    label="操作">
                <template slot-scope="scope">
                    <el-button type="text" @click="editForm(scope.row.id)">编辑</el-button>
                    <el-divider direction="vertical"></el-divider>
                    <el-popconfirm title="这是一段内容确定删除吗？" @confirm="delHandle(scope.row.id)">
                        <el-button type="text" slot="reference">删除</el-button>
                    </el-popconfirm>
                </template>
            </el-table-column>
        </el-table>
        <!--添加对话框-->
        <el-dialog
                title="提示"
                :visible.sync="dialogVisible"
                width="40%"
                :before-close="handleClose">
            <div class="dialogDiv">
                <el-form :model="addForm" :rules="rules" ref="addForm" label-width="100px" class="demo-addForm">
                    <el-form-item label="上级菜单" prop="parentId">
                        <el-select v-model="addForm.parentId === 0 ? null : addForm.parentId" placeholder="请选择上级菜单"
                                   clearable>
                            <template v-for="item in tableData">
                                <el-option :label="item.name" :value="item.id"></el-option>
                                <template v-for="child in item.children">
                                    <el-option :label="child.name" :value="child.id">
                                        {{' - ' + child.name}}
                                    </el-option>
                                </template>
                            </template>
                        </el-select>
                    </el-form-item>
                    <el-form-item label="菜单名称" prop="name">
                        <el-input v-model="addForm.name" placeholder="请输入菜单名称" clearable style="width: 60vh"></el-input>
                    </el-form-item>
                    <el-form-item label="权限编码" prop="perms">
                        <el-input v-model="addForm.perms" placeholder="请输入权限编码" clearable
                                  style="width: 60vh"></el-input>
                    </el-form-item>
                    <el-form-item label="图标" prop="icon">
                        <el-input v-model="addForm.icon" placeholder="请输入图标编码" clearable style="width: 60vh"></el-input>
                    </el-form-item>
                    <el-form-item label="菜单URL" prop="path">
                        <el-input v-model="addForm.path" placeholder="请输入菜单URL" clearable
                                  style="width: 60vh"></el-input>
                    </el-form-item>
                    <el-form-item label="菜单组件" prop="component">
                        <el-input v-model="addForm.component" placeholder="请输入菜单组件" clearable
                                  style="width: 60vh"></el-input>
                    </el-form-item>
                    <el-form-item label="类型" prop="type">
                        <el-radio-group v-model="addForm.type">
                            <el-radio :label=0>目录</el-radio>
                            <el-radio :label=1>菜单</el-radio>
                            <el-radio :label=2>按钮</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="状态" prop="status">
                        <el-radio-group v-model="addForm.status">
                            <el-radio :label=1>正常</el-radio>
                            <el-radio :label=0>禁用</el-radio>
                        </el-radio-group>
                    </el-form-item>
                    <el-form-item label="排序号" prop="orderNum">
                        <el-input-number v-model="addForm.orderNum" :min="1" label="排序号"></el-input-number>
                    </el-form-item>
                </el-form>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button @click="resetForm()">取 消</el-button>
                <el-button type="primary" @click="submitForm('addForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    export default {
        name: 'Menu',
        data() {
            return {
                dialogVisible: false,
                addForm: {
                    status: 1,
                    orderNum: 1
                },
                rules: {
                    name: [
                        {required: true, message: '请输入菜单名称', trigger: 'blur'}
                    ],
                    perms: [
                        {required: true, message: '请输入权限编码', trigger: 'blur'}
                    ],
                    type: [
                        {required: true, message: '请选择类型', trigger: 'blur'}
                    ],
                    status: [
                        {required: true, message: '请选择状态', trigger: 'change'}
                    ],
                    orderNum: [
                        {required: true, message: '请选择排序号', trigger: 'blur'}
                    ]
                },
                tableData: []
            }
        },
        created() {
            this.getMenuTree();
        },
        methods: {
            // 获取菜单列表
            getMenuTree() {
                this.$axios.get('/rookie/menu/list').then(res => {
                    this.tableData = res.data.result;
                });
            },
            // 新增/修改
            submitForm(formName) {
                this.$refs[formName].validate((valid) => {
                    if (valid) {
                        this.$axios.post('/rookie/menu/' + (this.addForm.id ? 'update' : 'save'), this.addForm).then(res => {
                            this.$message({
                                showClose: true,
                                message: '恭喜你，操作成功',
                                type: 'success',
                                center: true,
                                onClose: () => {
                                    this.getMenuTree();
                                }
                            });
                            this.resetForm();
                        });
                    } else return false;
                });
            },
            // 点击修改按钮
            editForm(id) {
                this.$axios.get('/rookie/menu/getInfoById/' + id).then(res => {
                    this.addForm = res.data.result;
                    this.dialogVisible = true;
                });
            },
            // 重置
            resetForm() {
                this.addForm = {
                    status: 1,
                    orderNum: 1
                };
                this.dialogVisible = false;
            },
            // 关闭对话框
            handleClose() {
                this.resetForm('addForm');
            },
            // 删除
            delHandle(id) {
                this.$axios.post('/rookie/menu/delete/' + id).then(res => {
                    this.$message({
                        showClose: true,
                        message: '恭喜你，操作成功',
                        type: 'success',
                        center: true,
                        onClose: () => {
                            this.getMenuTree();
                        }
                    });
                });
            }
        }
    }
</script>

<style>
    .dialogDiv {
        height: 50vh;
        overflow: auto;
    }
</style>